from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from api import models

class BlogAuthentication(BaseAuthentication):
    def authenticate(self, request):
        token = request.query_params.get('token')
        if not token:
            return
        instance = models.UserInfo.objects.filter(token=token).first()
        if not instance:
            return

        return instance,token
    def authenticate_header(self,request):
        return 'API'

class NoAuthentication(BaseAuthentication):
    def authenticate(self, request):
        raise AuthenticationFailed({"code":1004,"error":"认证失败"})

    def authenticate_header(self, request):
        return "API"